package com.example.fresh_fruit.controller;

import com.example.fresh_fruit.entity.Purchase;
import com.example.fresh_fruit.entity.PutHouseing;
import com.example.fresh_fruit.service.PurchaseService;
import com.example.fresh_fruit.service.PutHouseingService;
import com.example.fresh_fruit.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :飞天の鱼
 * @date : 2022/8/25 15:47
 */
@Slf4j
@RestController
@RequestMapping("/utHouseing")
public class PutHouseingController {

    @Autowired
    private PutHouseingService putHouseingService;

    @Autowired
    private PurchaseService purchaseService;

    @PostMapping("/insert")
    public Result insertPutHouseing(PutHouseing putHouseing){
        //根据前端传来的采购表实体类，获得采购表的ID，判断是否有采购表
        Integer purchaseId = putHouseing.getPurchaseId();
        Purchase purchase = purchaseService.selectById(purchaseId);
        if (purchase==null){
            return Result.fail("未找到此采购表");
        }//采购表存在
        //获得此采购表中的，商品id，商品数量
        Integer supplierId = purchase.getSupplierId();
        Double supplierNumber = purchase.getSupplierNumber();
        //将商品id，商品数量 设置进前端传来的
        putHouseing.setPurchaseId(supplierId);
        putHouseing.setPurchaseNumber(supplierNumber);

        int insertPutHouseing = putHouseingService.insertPutHouseing(putHouseing);
        if (insertPutHouseing!=1){
            return Result.fail("入库表生成失败");
        }
        return Result.ok("入库表生成成功");
    }

    @PutMapping("/updataState")
    private Result updataState(Integer id){
        //根据入库表的ID，查询是否存在此表
        int houseingServiceById = putHouseingService.findById(id);
        if (houseingServiceById!=0){
            return Result.fail("此入库表不存在！");
        }//此入库表存在后
        //修改入库表的状态，根据其ID
        int state = putHouseingService.updataPutHouseingState(id);
        if (state!=0){
            return Result.fail("入库表的状态修改失败！");
        }
        return Result.ok("入库表的状态修改成功！");
    }
}
